package org.example.controller;

import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;

import org.example.pojo.Book;
import org.example.service.BookFeign;
import org.example.service.OrderFeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author TonySong
 * @date 2024/5/15 0015
 * @time 14:08
 */
@RestController
@RequestMapping("/test")
@GlobalTransactional
public class OrderController {

    @Resource
    private BookFeign bookFeign;
    @Resource
    private OrderFeign orderFeign;


    @GetMapping("/order/{uid}/{bid}")
    public String downOrder(@PathVariable("uid") Long uid,@PathVariable("bid") Long bid) {
       //根据图书id查询图书
        Book book= this.bookFeign.findById(bid);
        //判断图书的库存是否大于0
        if(book.getAmount()> 0 ){
           Boolean f1=  this.orderFeign.pay(uid,bid);
           Boolean f2=   this.bookFeign.updateAmount(bid);
           if(f1 && f2) {
              // int m= 11/0;
               return "创建订单成功！";
           }
        }
        return "下单失败！";

    }
}
